<?php

/**
 * material actions.
 *
 * @package    zing
 * @subpackage material
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class materialActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
//    $this->materials = Doctrine_Core::getTable('Material')
//      ->createQuery('a')
//      ->execute();
    
    $this->getContext()->getInstance()->getConfiguration()->loadHelpers("Url"); 


        $table = new sfTable(array('name'=>'tMaterial','model'=>'material'),array('class'=>'pedidos')); //border:1px solid;',
        $table->thSchema->setDefaultSort('nombre');
        $table->thSchema->setDefaultSortDirection('asc');         
        $table->tdSchema->setOption('defaultTdAttributes',array('style'=>"text-align: center;width: 84px; overflow:hidden;margin-left:3px"));         

        //------
        $table->thSchema['id'] = new sfTh(array('text'=>'Cod. Material'));
        $table->thSchema['id']->setFilterType('number')
                             ->setFilterable(true,'Codigo Material');
        $table->tdSchema['id'] = new sfTdModel(array('field'=>'id'));         

        //------        
        $table->thSchema['nombre'] = new sfTh(array('text'=>'Material'));    
        $table->thSchema['nombre']->setFilterable(true,'Material')
                                ->setFilterType('text')
                                ->setOption('multiText',true);
        $table->tdSchema['nombre'] = new sfTdModel(array('field'=>'nombre'));
        

        $table->thSchema['unidad'] = new sfTh(array('text'=>'Unidad'));    
        $table->thSchema['unidad']->setFilterable(false);
        $table->thSchema['unidad']->setSortable(false);        
        $table->tdSchema['unidad'] = new sfTdModel(array('field'=>'unidad'));

        
        $table->thSchema['stockMinimo'] = new sfTh(array('text'=>'Stock Min.'));    
        $table->thSchema['stockMinimo']->setFilterable(false);
        $table->thSchema['stockMinimo']->setSortable(false);        
        $table->tdSchema['stockMinimo'] = new sfTdModel(array('field'=>'stockMinimo'));

        
        $table->thSchema['stockRequerido'] = new sfTh(array('text'=>'Stock Reque.'));    
        $table->thSchema['stockRequerido']->setFilterable(false);
        $table->thSchema['stockRequerido']->setSortable(false);        
        $table->tdSchema['stockRequerido'] = new sfTdModel(array('field'=>'stockRequerido'));
        
        
        $table->thSchema['stockDeseado'] = new sfTh(array('text'=>'Stock Desea.'));    
        $table->thSchema['stockDeseado']->setFilterable(false);
        $table->thSchema['stockDeseado']->setSortable(false);        
        $table->tdSchema['stockDeseado'] = new sfTdModel(array('field'=>'stockDeseado'));
        

        $table->thSchema['stockReal'] = new sfTh(array('text'=>'Stock Real'));    
        $table->thSchema['stockReal']->setFilterable(false);
        $table->thSchema['stockReal']->setSortable(false);        
        $table->tdSchema['stockReal'] = new sfTdModel(array('field'=>'stockReal'));
        
        
        $table->thSchema['precioUnidad'] = new sfTh(array('text'=>'Precio'));    
        $table->thSchema['precioUnidad']->setFilterable(false);
        $table->thSchema['precioUnidad']->setSortable(false);        
        $table->tdSchema['precioUnidad'] = new sfTdModel(array('field'=>'precioUnidad'));
                
//------               
        
        $table->thSchema['herramientas'] = new sfTh(array('text'=>'Herramientas','sortable'=>false),array('style' => 'text-align: center; width:50px;'));                
        $table->setPageSize(15);
        $table->setAction('material/index');
        $table->buildQuery($request);        
        $table->execute($request);        
        
        $this->tabla =  $table;
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MaterialForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MaterialForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($material = Doctrine_Core::getTable('Material')->find(array($request->getParameter('id'))), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $this->form = new MaterialForm($material);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($material = Doctrine_Core::getTable('Material')->find(array($request->getParameter('id'))), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $this->form = new MaterialForm($material);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($material = Doctrine_Core::getTable('Material')->find(array($request->getParameter('id'))), sprintf('Object material does not exist (%s).', $request->getParameter('id')));
    $material->delete();

    $this->redirect('material/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $material = $form->save();

      $this->redirect('material/edit?id='.$material->getId());
    }
  }
}
